package cn.xiongxf.mq.consumer;

import cn.xiongxf.service.IPayOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * MQ事务消息【订单超时处理】消费者 - 支付服务
 */
@Component
@Slf4j
@RocketMQMessageListener(
        consumerGroup = "service-order-consumer-payexpire",
        topic = "topic-payexpire",
        selectorExpression = "tags-payexpire",
        messageModel = MessageModel.BROADCASTING  // 广播模式消费
)
public class PayExpireForPayConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IPayOrderService payOrderService;

    @Override
    public void onMessage(MessageExt messageExt) {
        if(messageExt.getBody() == null || messageExt.getBody().length == 0) return;
        // 解析结果对象
        String orderNo = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        payOrderService.cancelOrder(orderNo);
        log.info("订单超时处理：{}", orderNo);
    }
}
